package com.wyl;


//import org.apache.commons.io.FileUtils;
//import org.apache.poi.sl.usermodel.PictureData;
//import org.apache.poi.sl.usermodel.TableCell;
//import org.apache.poi.sl.usermodel.TextParagraph;
//import org.apache.poi.sl.usermodel.VerticalAlignment;
//import org.apache.poi.xslf.usermodel.*;
//
//import java.awt.*;
//import java.awt.geom.Rectangle2D;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;

import cn.hutool.core.io.FileUtil;
import org.apache.poi.xslf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
 * @author 吴宇亮 on 2023/4/13 13:46
 * poi jar包说明
 *
 * 用于操作.xls文件
 * poi-version
 *
 * 用于操作.xlsx、.pptx、docx文件
 * poi-ooxml
 * poi-ooxml-schemas
 *
 * 例子，里面有main方法可以直接运行看效果
 * poi-examples
 *
 * 用于操作.ppt、.doc、.vsd、.pub、.msg文件，依赖于poi
 * poi-scratchpad
 */
public class ExportPPT {


    public static void main(String[] args) throws Exception {
        // 创建空的PowerPoint文档
        XMLSlideShow ppt = new XMLSlideShow();
        // 加载要插入的图像文件
        String filePath = "C:\\Users\\Administrator\\Pictures\\1.png";
        File file = new File(filePath);
        BufferedImage img = ImageIO.read(file);
        // 将图像数据添加到PPT中
        int pictureData = ppt.addPicture(FileUtil.readBytes(file), 5);

        for (int i = 0; i < 3; i++) {
            // 创建幻灯片并添加到文档中
            XSLFSlide slide = ppt.createSlide();
            // 获取主题区域的大小，居中显示图片
            Dimension pageSize = slide.getSlideShow().getPageSize();
            int width = img.getWidth();
            int height = img.getHeight();
            if (width > pageSize.width || height > pageSize.height) {
                double scale = Math.min((double)pageSize.width / width, (double)pageSize.height / height);
                width = (int)(width * scale);
                height = (int)(height * scale);
            }
            // 在幻灯片上绘制图像
            XSLFPictureShape picture = slide.createPicture(pictureData);
            picture.setAnchor(new java.awt.Rectangle( (pageSize.width - width) / 2, (pageSize.height - height) / 2, width, height));
        }
        // 将PPT文件保存到磁盘上
        FileOutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\TEST3.pptx");
        ppt.write(out);
        out.close();
    }

    // 注释原因：下面代码依赖：poi 5.2.2
//    public static void main(String[] args) throws IOException {
//        XMLSlideShow ppt = new XMLSlideShow();
//        XSLFSlide slide = ppt.createSlide();//创建幻灯片
//        XSLFTextBox textBox = slide.createTextBox();
//        textBox.setAnchor(new Rectangle2D.Double(10,10, 200, 30));
//        textBox.addNewTextParagraph().addNewTextRun().setText("创建幻灯片");
//
//        Object[][] datas = {{"区域产品销售额","",""},{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045,  2256}, {"广东省", 3000, 690}};
//        XSLFTable table = slide.createTable();//创建表格
//        table.setAnchor(new Rectangle2D.Double(10, 50, 0, 0));
//        for(int i = 0; i < datas.length; i++) {
//            XSLFTableRow tableRow = table.addRow(); //创建表格行
//            for(int j = 0; j < datas[i].length; j++) {
//                XSLFTableCell tableCell = tableRow.addCell();//创建表格单元格
//                XSLFTextParagraph p = tableCell.addNewTextParagraph();
//                XSLFTextRun tr = p.addNewTextRun();
//                tr.setText(String.valueOf(datas[i][j]));
//
//                tableCell.setFillColor(Color.getColor("0xdd7e6b"));
//                p.setTextAlign(TextParagraph.TextAlign.CENTER);
//                tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
//
//                if(i == datas.length - 1 && j == 3-1) {
//                    tr.setFontSize(16d);
//                    tr.setBold(true);
//                    tr.setItalic(true);
//                    tr.setUnderlined(true);
//                    tr.setFontFamily("\u5b8b\u4f53");
//                    tr.setFontColor(Color.RED);
//                }
//                tableCell.setBorderWidth(TableCell.BorderEdge.left,1);
//                tableCell.setBorderWidth(TableCell.BorderEdge.right,1);
//                tableCell.setBorderWidth(TableCell.BorderEdge.top,1);
//                tableCell.setBorderWidth(TableCell.BorderEdge.bottom,1);
//                tableCell.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK);
//                tableCell.setBorderColor(TableCell.BorderEdge.left,Color.BLACK);
//                tableCell.setBorderColor(TableCell.BorderEdge.right,Color.BLACK);
//                tableCell.setBorderColor(TableCell.BorderEdge.top,Color.BLACK);
//            }
//            tableRow.setHeight(30);
//        }
//
//        //设置列宽
//        table.setColumnWidth(0, 150);
//        table.setColumnWidth(1, 150);
//        table.setColumnWidth(2, 250);
//
//        //合并单元格
//        table.mergeCells(0, 0, 0, 2);
//
//        //插入图片
//        byte[] bt = FileUtils.readFileToByteArray(new File("C:\\Users\\Administrator\\Pictures\\1.png"));
//        XSLFPictureData idx = ppt.addPicture(bt, PictureData.PictureType.PNG);
//        XSLFPictureShape pic = slide.createPicture(idx);
//        pic.setAnchor(new Rectangle2D.Double(10, 200, 339, 197));
//
//        //创建一个文本链接
//        XSLFTextBox linkText = slide.createTextBox();
//        linkText.setAnchor(new Rectangle2D.Double(430, 310, 80, 30));
//        XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();
//        r.setText("Apache POI");
//        XSLFHyperlink link = r.createHyperlink();
//        link.setAddress("http://poi.apache.org");
//        ppt.write(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\AddImageToPPT.pptx"));
//    }
}
